﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties.CApSLPSetLocationsPropertiesSourceLocationCommomPropertiesUtils
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using C1.Win.C1FlexGrid;
using C1.Win.C1FlexGrid.Util.BaseControls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;
using Tecnomatix.Engineering.Olp.RoboticParameters;
using Tecnomatix.Engineering.Ui48;


namespace DnProcessSimulateCommands.MultiEditingInPathEditor.SetLocationsProperties;

internal class CApSLPSetLocationsPropertiesSourceLocationCommomPropertiesUtils
{
  private TxFlexGrid _txFGSourceCommonProperties;
  private TxOlpRoboticParametersManager _roboticManager;
  private HasField _hasFieldReference;
  private EnableCopyButton _enableCopyButtonReference;
  private IsFieldReadOnly _isFieldReadOnlyReference;
  private IsFieldBlockedForCopy _isFieldBlockedForCopyReference;
  private bool _enableSelection = true;
  private bool _externalSelectionMode;
  private Row _selectedRow;
  private const string _CUSTOMIZED_MOTION = "Customized Motion";

  private bool ExternalSelectionMode
  {
    set => this._externalSelectionMode = value;
    get => this._externalSelectionMode;
  }

  public CApSLPSetLocationsPropertiesSourceLocationCommomPropertiesUtils(
    TxFlexGrid sourceCommonProperties,
    TxOlpRoboticParametersManager roboticManager,
    HasField hasFieldReference,
    EnableCopyButton enableCopyButtonReference,
    IsFieldReadOnly isFieldReadOnlyReference,
    IsFieldBlockedForCopy isFieldBlockedForCopy)
  {
    this._txFGSourceCommonProperties = sourceCommonProperties;
    this._roboticManager = roboticManager;
    this._hasFieldReference = hasFieldReference;
    this._enableCopyButtonReference = enableCopyButtonReference;
    this._isFieldReadOnlyReference = isFieldReadOnlyReference;
    this._isFieldBlockedForCopyReference = isFieldBlockedForCopy;
    this.RegisterEvents();
  }

  public void RegisterEvents()
  {
    ((Control) this._txFGSourceCommonProperties).KeyDown += new KeyEventHandler(this._txFGSourceCommonProperties_KeyDown);
    ((C1FlexGridBase) this._txFGSourceCommonProperties).AfterSelChange += new RangeEventHandler(this._txFGSourceCommonProperties_AfterSelChange);
  }

  public void UnRegisterEvents()
  {
    ((Control) this._txFGSourceCommonProperties).KeyDown -= new KeyEventHandler(this._txFGSourceCommonProperties_KeyDown);
    ((C1FlexGridBase) this._txFGSourceCommonProperties).AfterSelChange -= new RangeEventHandler(this._txFGSourceCommonProperties_AfterSelChange);
  }

  public void InitializeList() => this.CreateSourceCommonPropertiesStructure();

  private void CreateSourceCommonPropertiesStructure()
  {
    ((C1FlexGridBase) this._txFGSourceCommonProperties).Cols[0].Name = ((C1FlexGridBase) this._txFGSourceCommonProperties).Cols[0].Caption = StringTable.ResourceManager.GetString("SLP_CommonListPropertyTitle");
    ((C1FlexGridBase) this._txFGSourceCommonProperties).Cols[1].Name = ((C1FlexGridBase) this._txFGSourceCommonProperties).Cols[1].Caption = StringTable.ResourceManager.GetString("SLP_CommonListValueTitle");
  }

  public void FillCommonPropertiesWithContent(ITxRoboticLocationOperation sourceLocation)
  {
    List<string> failedPropertyNames = new List<string>(0);
    ((ScrollableControl) this._txFGSourceCommonProperties).BeginUpdate();
    TxPathEditorToDotNetGap editorToDotNetGap = (TxPathEditorToDotNetGap) null;
    try
    {
      editorToDotNetGap = new TxPathEditorToDotNetGap();
      editorToDotNetGap.StartCacheMode();
      this.ClearList();
      this._enableSelection = false;
      foreach (KeyValuePair<string, string> property in this._roboticManager.GetProperties())
      {
        if (this._hasFieldReference(property.Value))
        {
          Row row = ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows.Add();
          TxOlpDataValueBase dataValue = this._roboticManager.GetDataValue(property.Key);
          bool flag = false;
          object obj;
          try
          {
            obj = dataValue.GetValue((ITxOperation) sourceLocation);
            if (obj is ITxObject)
              obj = (object) (obj as ITxObject).Name;
            else if (property.Key.Contains("Customized Motion"))
              obj = (object) dataValue.ToString((ITxOperation) sourceLocation);
            else if (obj != null)
              obj = (object) obj.ToString();
          }
          catch (Exception ex)
          {
            flag = true;
            obj = (object) string.Empty;
            failedPropertyNames.Add(property.Value);
          }
          row[0] = (object) property.Value;
          if (!flag)
          {
            object fixedValue = (object) null;
            string displayFormat = string.Empty;
            Type valueType = this._roboticManager.GetValueType(property.Key);
            CApSLPSetLocationsPropertiesLogic.GetRowFormat(property.Key, obj, this._roboticManager.GetUnitType(property.Key), out displayFormat, out fixedValue);
            row[1] = fixedValue;
            if (!string.IsNullOrEmpty(displayFormat))
              ((RowCol) row).Format = displayFormat;
            if (valueType != (Type) null)
              ((RowCol) row).DataType = valueType;
            ((RowCol) row).AllowEditing = false;
            ((RowCol) row).Style.TextAlign = (TextAlignEnum) 1;
            if (this._isFieldReadOnlyReference(property.Value) && property.Value != "Config")
            {
              ((C1FlexGridBase) this._txFGSourceCommonProperties).SetCellStyle(((RowCol) row).Index, 0, ((C1FlexGridBase) this._txFGSourceCommonProperties).Styles["ReadOnly"]);
              ((C1FlexGridBase) this._txFGSourceCommonProperties).SetCellStyle(((RowCol) row).Index, 1, ((C1FlexGridBase) this._txFGSourceCommonProperties).Styles["ReadOnly"]);
            }
          }
          else
          {
            row[1] = (object) string.Empty;
            Type valueType = this._roboticManager.GetValueType(property.Key);
            ((RowCol) row).Format = string.Empty;
            if (valueType != (Type) null)
              ((RowCol) row).DataType = valueType;
            ((RowCol) row).AllowEditing = false;
            ((RowCol) row).Style.TextAlign = (TextAlignEnum) 1;
            ((C1FlexGridBase) this._txFGSourceCommonProperties).SetCellStyle(((RowCol) row).Index, 0, ((C1FlexGridBase) this._txFGSourceCommonProperties).Styles["ReadOnly"]);
            ((C1FlexGridBase) this._txFGSourceCommonProperties).SetCellStyle(((RowCol) row).Index, 1, ((C1FlexGridBase) this._txFGSourceCommonProperties).Styles["ReadOnly"]);
          }
        }
      }
      this._enableSelection = true;
    }
    finally
    {
      editorToDotNetGap?.EndCacheMode();
      GC.KeepAlive((object) editorToDotNetGap);
      ((ScrollableControl) this._txFGSourceCommonProperties).EndUpdate();
      if (failedPropertyNames.Count > 0)
        this.ShowErrorForPropertiesRead(failedPropertyNames);
    }
  }

  public void ClearList()
  {
    if (((RowColCollection) ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows).Count <= 1)
      return;
    ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows.RemoveRange(1, ((RowColCollection) ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows).Count - 1);
  }

  private void SelectAllLocationsTo(Row row)
  {
    int index1;
    int index2;
    if (((RowCol) this._selectedRow).Index <= ((RowCol) row).Index)
    {
      index1 = ((RowCol) this._selectedRow).Index;
      index2 = ((RowCol) row).Index;
    }
    else
    {
      index1 = ((RowCol) row).Index;
      index2 = ((RowCol) this._selectedRow).Index;
    }
    for (int index3 = index1; index3 <= index2; ++index3)
      ((RowCol) ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows[index3]).Selected = true;
  }

  private void SelectAll()
  {
    this._selectedRow = ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows[1];
    this.SelectAllLocationsTo(((C1FlexGridBase) this._txFGSourceCommonProperties).Rows[((RowColCollection) ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows).Count - 1]);
  }

  private void _txFGSourceCommonProperties_KeyDown(object sender, KeyEventArgs e)
  {
    if (Control.ModifierKeys != Keys.Control || e.KeyCode != Keys.A)
      return;
    this.SelectAll();
  }

  private void _txFGSourceCommonProperties_AfterSelChange(object sender, RangeEventArgs e)
  {
    bool shouldEnable = false;
    if (this._enableSelection)
    {
      for (int index = 1; index < ((RowColCollection) ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows).Count; ++index)
      {
        Row row = ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows[index];
        if (((RowCol) row).Index > 0)
        {
          string stringName = ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows[index][0].ToString();
          bool flag1 = this._isFieldReadOnlyReference(stringName) && stringName != "Config";
          bool flag2 = this._isFieldBlockedForCopyReference(stringName);
          if (((RowCol) row).Selected && !flag1 && !flag2)
            shouldEnable = true;
        }
      }
    }
    this._enableCopyButtonReference(shouldEnable);
  }

  public List<string> GetAllSelectedProperties()
  {
    List<string> selectedProperties = new List<string>();
    foreach (Row row in (IEnumerable) ((C1FlexGridBase) this._txFGSourceCommonProperties).Rows)
    {
      if (((RowCol) row).Selected)
      {
        string propertyId = this._roboticManager.GetPropertyId(row[0] as string);
        if (!string.IsNullOrEmpty(propertyId))
          selectedProperties.Add(propertyId);
      }
    }
    return selectedProperties;
  }

  private void ShowErrorForPropertiesRead(List<string> failedPropertyNames)
  {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.AppendLine(StringTable.SLP_MSG_SOURCE_LOCATION_FAILED_PROPERTIES_READ);
    bool flag = true;
    foreach (string failedPropertyName in failedPropertyNames)
    {
      if (flag)
        flag = false;
      else
        stringBuilder.Append(", ");
      stringBuilder.Append(failedPropertyName);
    }
    int num = (int) TxDoNotShowAgainMessageBox.Show(stringBuilder.ToString(), StringTable.SET_LOCATIONS_PROPERTIES_NAME, "SLP_MSG_SOURCE_LOCATION_FAILED_PROPERTIES_READ", MessageBoxButtons.OK, MessageBoxIcon.Hand, DialogResult.OK);
  }
}
